package com.dwj.auto.comment.handler;

import com.dwj.auto.comment.common.enums.CommentType;
import com.dwj.auto.comment.common.enums.ConjunctionEnum;
import com.dwj.auto.comment.common.enums.TempLevelEnum;
import com.dwj.auto.comment.model.dto.CommentResultDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassName StatementJoinHandler
 * @Description 语句衔接处理器
 * @Author dwjian
 * @Date 2020/7/19 11:42
 */
public class ConjunctionHandler {

    public static void handle(List<CommentResultDto> list) {
        if (!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                //跳过期望
                if(list.get(i).getType().equals(CommentType.EXPECT.getCode())){
                    continue;
                }
                //转折词
                if (i > 0) {
                    String preLevel = list.get(i - 1).getLevel();
                    String level = list.get(i).getLevel();
                    String preContent = list.get(i - 1).getContent();
                    if (checkTurningWords(preLevel, level) && !containsTurningWords(preContent)) {
                        String but = ConjunctionEnum.random(ConjunctionEnum.BUT);
                        list.get(i).setContent(StringUtils.join(but, list.get(i).getContent()));
                    }
                }
                //并列连词
                if (i > 1) {
                    String preTwoLevel = list.get(i - 2).getLevel();
                    String preLevel = list.get(i - 1).getLevel();
                    String level = list.get(i).getLevel();
                    String preTwoContent = list.get(i - 2).getContent();
                    String preContent = list.get(i - 1).getContent();
                    if (checkConjunction(preTwoLevel, preLevel, level) && !containsConjunction(preTwoContent, preContent)) {
                        String but = ConjunctionEnum.random(ConjunctionEnum.AND);
                        list.get(i).setContent(StringUtils.join(but, list.get(i).getContent()));
                    }
                }
            }
        }
    }

    private static boolean checkTurningWords(String preLevel, String level) {
        if ((preLevel.equals(TempLevelEnum.EXCELLENT.name()) || preLevel.equals(TempLevelEnum.GOOD.name()))
                && level.equals(TempLevelEnum.UNQUALIFIED.name())) {
            return true;
        }
        return (level.equals(TempLevelEnum.EXCELLENT.name()) || level.equals(TempLevelEnum.GOOD.name()))
                && preLevel.equals(TempLevelEnum.UNQUALIFIED.name());
    }

    private static boolean checkConjunction(String preTwoLevel, String preLevel, String level) {
        if ((preTwoLevel.equals(TempLevelEnum.EXCELLENT.name()) || preTwoLevel.equals(TempLevelEnum.GOOD.name()))
                && (preLevel.equals(TempLevelEnum.EXCELLENT.name()) || preLevel.equals(TempLevelEnum.GOOD.name()))
                && (level.equals(TempLevelEnum.EXCELLENT.name()) || level.equals(TempLevelEnum.GOOD.name()))) {
            return true;
        }
        return preTwoLevel.equals(TempLevelEnum.UNQUALIFIED.name())
                && preLevel.equals(TempLevelEnum.UNQUALIFIED.name())
                && level.equals(TempLevelEnum.UNQUALIFIED.name());
    }

    private static boolean containsTurningWords(String preContent){
        for (String word : ConjunctionEnum.BUT.getValue().split(",")) {
            if(preContent.contains(word)){
                return true;
            }
        }
        return false;
    }

    private static boolean containsConjunction(String preTwoLContent, String preContent){
        for (String word : ConjunctionEnum.AND.getValue().split(",")) {
            if(preTwoLContent.contains(word) || preContent.contains(word)){
                return true;
            }
        }
        return false;
    }
}
